package main.java.cn.wolfcode.web.controller;


import cn.wolfcode.domain.Appointment;
import cn.wolfcode.enums.AppointmentStatusEnum;
import cn.wolfcode.qo.AppointmentQueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private IBusinessService businessService;


    @RequiredPermission(name="门店预约页面",expression="appointment:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") AppointmentQueryObject qo){
        PageInfo<Appointment> pageInfo = appointmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("businesses", businessService.listAll());
        //共享状态的枚举实例
        model.addAttribute("enums", AppointmentStatusEnum.values());
        return "appointment/list";
    }


    @RequiredPermission(name="门店预约删除",expression="appointment:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           appointmentService.delete(id);
        }
        return "redirect:/appointment/list";
    }


    @RequiredPermission(name="门店预约新增/编辑",expression="appointment:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Appointment appointment) {
        if (appointment.getId() == null) {
            //如果是客服主动添加的状态就设置为履行中
            //因为在修改中时间,流水号,状态是不需要修改的,所以删除该字段就可以
            appointment.setStatus(AppointmentStatusEnum.PERFORM.getValue());
            appointmentService.save(appointment);
        } else {
            appointmentService.update(appointment);
        }
        return "redirect:/appointment/list";
    }


    @RequiredPermission(name="门店预约新增/编辑",expression="appointment:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("appointment",appointmentService.get(id));
        }
        return "appointment/input";
    }


    //如果是客户网上预约的就设置状态为待确定

    @RequestMapping("/save")
    public String save(Appointment appointment,Model model) {
        //设置为待确认状态
        appointment.setStatus(AppointmentStatusEnum.PEND.getValue());
        appointmentService.save(appointment);
        return "redirect:/index";
    }


    //预约单状态更新

    @RequiredPermission(name="预约单状态更新",expression="appointment:updateStatus")
    @RequestMapping("/updateStatus")
    public String updateStatus(Long id,Integer status) {
        appointmentService.updateStatus(id,status);
        return "redirect:/appointment/list";
    }

}
